﻿using Application.AppService;
using AutoMapper;
using Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Data.SqlClient;
using static System.Net.Mime.MediaTypeNames;

namespace UI2.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class StudentController : Controller
	{
		private readonly IStudentAppService studentAppService;
		private readonly IMapper mapper;

		public StudentController(IStudentAppService studentAppService, IMapper mapper)
		{
			this.studentAppService = studentAppService;
			this.mapper = mapper;
		}

		public IActionResult Index()
		{
			return View();
		}
		public IActionResult Index1()
		{
			List<Student> trees = studentAppService.StudentSelectAsync();
			return Json(trees);
		}
		[HttpPut]
		public async Task<IActionResult> Update(Student student)
		{
			Student student2 = mapper.Map<Student>(student);

			int result = await studentAppService.StudentUpdateAsync(student2);

			return Json(result);
		}

		[HttpDelete]
		public async Task<IActionResult> Delete(int Id)
		{
			int result = await studentAppService.StudentDeleteAsync(Id);
			if (result>0)
			{
				return Json(result);
			}
			else
			{
				return RedirectToAction("Index");                //return View(tree);
			}
		}

		[HttpPost]
		public async Task<IActionResult> Insert(StudentAddDto studentAddDto)
		{
			Student student = mapper.Map<Student>(studentAddDto);

			int result = await studentAppService.StudentAddAsync(student);

			return Json(result);
		}
	}
}
